﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMovieSystem
{
    public partial class FrmMoviePlay : Form
    {
        public FrmMoviePlay()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 影片对象
        /// </summary>
        public Movie movie = new Movie();
        /// <summary>
        /// 临时数据集
        /// </summary>
        List<Hall> listHall = new List<Hall>();
        /// <summary>
        /// 临时存储影厅信息
        /// </summary>
        Hall hall = new Hall();

        #region 加载事件
        //加载事件
        private void FrmMoviePlay_Load(object sender, EventArgs e)
        {
            textBox1.Text = movie.MovieName;
            dateTimePicker1.MinDate = DateTime.Now.Date;
            dateTimePicker2.MinDate = DateTime.Now.Date;
            panel1.BringToFront();
            BindDgvAll();
            BindCmb();
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定dgv
        /// </summary>
        private void BindData()
        {
            listHall.Clear();
            label3.Visible = false;
            DateTime time = dateTimePicker1.Value;
            foreach (Hall item in movie.HallInfo)
            {
                if (item.ShowData.ToShortDateString() == time.ToShortDateString())
                {
                    listHall.Add(item);
                }
            }
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Hall>(listHall);
            if (dataGridView1.RowCount < 1)
                label3.Visible = true;
        }

        /// <summary>
        /// 绑定所有数据
        /// </summary>
        private void BindDgvAll()
        {
            label3.Visible = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = new BindingList<Hall>(movie.HallInfo);
            if (dataGridView1.RowCount < 1)
                label3.Visible = true;
        }

        /// <summary>
        /// 绑定combobox
        /// </summary>
        private void BindCmb()
        {
            comboBox1.DataSource = new BindingList<Hall>(MovieManage.dirHall.Values.ToList());
            comboBox1.ValueMember = "H_ID";
            comboBox1.DisplayMember = "H_Name";
        }

        //修改日期控件值发生
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            BindData();
        }
        //全部场次
        private void button2_Click(object sender, EventArgs e)
        {
            BindDgvAll();
        }

        #endregion

        #region 新增
        //新增
        private void AddScheduleButton_Click(object sender, EventArgs e)
        {
            hall = new Hall();
            panel1.Visible = true;
            label9.Text = "新增影厅";
            button4.Text = "新增";
            comboBox1.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            dateTimePicker2.Value = DateTime.Now;
            comboBox2.Text = "00";
            comboBox3.Text = "00";
        }
        #endregion

        #region 修改
        //修改
        private void ModefyButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount < 1)
            {
                MessageBox.Show("请选择有效的数据行！","提示：");
                return;
            }
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string time = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            DateTime date = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[3].Value);
            panel1.Visible = true;
            label9.Text = "修改影厅";
            button4.Text = "修改";
            foreach (Hall item in movie.HallInfo)
            {
                if (item.H_ID == id && item.ShowData == date &&
                    item.ShowTime == time)
                {
                    comboBox1.Text = item.H_Name;
                    comboBox4.Text = item.H_Type;
                    dateTimePicker2.Value = item.ShowData;
                    comboBox2.Text=time.Substring(0,time.IndexOf(':'));
                    comboBox3.Text = time.Substring(time.IndexOf(':')+1);
                    hall = item;
                    break;
                }
            }
        }
        #endregion

        #region 新增/修改
        //新增/修改
        private void button4_Click(object sender, EventArgs e)
        {
            Hall hall01 = new Hall();
            ObjectAssign(hall01);
            foreach (Hall item in movie.HallInfo)
            {
                if (item.H_ID == hall01.H_ID && item.ShowData == hall01.ShowData
                    && item.ShowTime == hall01.ShowTime)
                {
                    MessageBox.Show("该时间段场次已存在！", "提示：");
                    return;
                }
            }
            if (button4.Text == "新增")
            {
                movie.HallInfo.Add(hall01);
                MessageBox.Show("添加成功！", "提示：");
            }
            else if (button4.Text == "修改")
            {
                ObjectAssign(hall);
                MessageBox.Show("修改成功！", "提示：");
            }
            panel1.Visible = false;
            BindDgvAll();
        }
        #endregion

        #region 取出控件文本赋值给对象
        /// <summary>
        /// 取出控件文本赋值给对象
        /// </summary>
        private void ObjectAssign(Hall hall)
        {
            hall.H_ID = comboBox1.SelectedValue.ToString();
            hall.H_Name = comboBox1.Text;
            hall.H_Type = comboBox4.Text;
            hall.ShowData = dateTimePicker2.Value.Date;
            hall.ShowTime = comboBox2.Text + ":" + comboBox3.Text;
            hall.Price = movie.MoviePrice;
            hall.SeatInfo = MovieManage.dirHall[hall.H_ID].SeatInfo;
        }
        #endregion

        #region 删除
        //删除
        private void DelButton_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            DateTime date = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[3].Value);
            string time = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            foreach (Hall item in movie.HallInfo)
            {
                if (item.H_ID == id && item.ShowData == date&&item.ShowTime == time)
                {
                    DialogResult r = MessageBox.Show("确定将该场次永久删除吗？","提示：",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                    if (r != DialogResult.OK)
                        return;
                    movie.HallInfo.Remove(item);
                    break;
                }
            }
            BindDgvAll();
        }
        #endregion

        //取消
        private void button3_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        //关闭
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
